/**
 * 功能:文件模板管理表 控制器类
 * 开发人员:yhb
 * 创建时间:2011-6-9 17:03:27
 */
package com.towker.frame.controller.sys;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.model.sys.FileTemplate;
import com.towker.frame.service.sys.filetemplate.FileTemplateService;
import com.towker.web.MyRequest;

//文件模板管理表
@Controller
@RequestMapping("/manage/sys/fileTemplate")
public class FileTemplateController
{
	private FileTemplateService service;
	public void setFileTemplateService(FileTemplateService service){this.service = service;}
	//添加
	@RequestMapping("/addFileTemplate{viewName}")
	public ModelAndView addFileTemplate(HttpServletResponse response, FileTemplate po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName)){
			result = new ModelAndView();
			result.setViewName("/manage/sys/fileTemplate/addFileTemplate.jsp");
		}else if ("2".equals(viewName)){
			PrintWriter writer = response.getWriter();
			try{
				service.add(po);
				writer.write("添加文件模板管理表成功！");
			}catch (Exception e){e.printStackTrace();writer.write("添加文件模板管理表失败！" + e.getCause());}
		}
		return result;
	}
	//删除
	@RequestMapping("/delFileTemplate")
	public void delFileTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}
	//修改
	@RequestMapping("/updFileTemplate{viewName}")
	public ModelAndView updFileTemplate(HttpServletRequest request, HttpServletResponse response, FileTemplate po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName)){
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			result.addObject("filetemplate", po);
			result.setViewName("/manage/sys/fileTemplate/updFileTemplate.jsp");
		}else if ("2".equals(viewName)){
			PrintWriter writer = response.getWriter();
			try{
				service.update(po);
				writer.write("修改文件模板管理表成功！");
			}catch (Exception e){e.printStackTrace();writer.write("修改文件模板管理表失败！" + e.getCause());}
		}
		return result;
	}
	//获得分页
	@SuppressWarnings("unchecked")
	@RequestMapping("/getFileTemplate")
	public ModelAndView getFileTemplate(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page){
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		PageModel<FileTemplate> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/sys/fileTemplate/getFileTemplate.jsp");
		return result;
	}
	//明细
	@RequestMapping("/getFileTemplateById")
	public ModelAndView getFileTemplateById(HttpServletRequest request, HttpServletResponse response){
		FileTemplate po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("filetemplate", po);
		result.setViewName("/manage/sys/fileTemplate/getFileTemplateById.jsp");
		return result;
	}
}
